package com.ruoyi.web.controller.module.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.customer.service.ICustomerUserService;
import com.ruoyi.order.domain.OrderCashDistribution;
import com.ruoyi.order.service.IOrderCashDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分销提现订单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/order/cash/distribution/")
public class OrderCashDistributionController extends BaseController
{
    @Autowired
    private IOrderCashDistributionService orderCashDistributionService;

    @Autowired
    private ICustomerUserService customerUserService;

    /**
     * 获取分销提现订单列表
     */
    @PostMapping("/orderCashDistributionList")
    public TableDataInfo list(@RequestBody OrderCashDistribution orderCashDistribution, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderCashDistribution.setShopId(shopId);
        }
        startPageByPost();
        List<OrderCashDistribution> list = orderCashDistributionService.selectOrderCashDistributionList(orderCashDistribution);
        return getDataTable(list);
    }

    @Log(title = "分销提现订单管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('order:cash:customer:export')")
    @PostMapping("/orderCashDistributionExport")
    public void export(HttpServletResponse response, OrderCashDistribution orderCashDistribution)
    {
        List<OrderCashDistribution> list = orderCashDistributionService.selectOrderCashDistributionList(orderCashDistribution);
        ExcelUtil<OrderCashDistribution> util = new ExcelUtil<OrderCashDistribution>(OrderCashDistribution.class);
        util.exportExcel(response, list, "分销提现订单数据");
    }

    /**
     * 根据分销提现订单编号获取详细信息
     */
    @GetMapping(value = { "/", "/{orderNo}" })
    public AjaxResult getInfo(@PathVariable(value = "orderNo", required = false) String orderNo)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(orderNo))
        {
            OrderCashDistribution orderCashDistribution = orderCashDistributionService.selectOrderCashDistributionByOrderNo(orderNo);
            ajax.put(AjaxResult.DATA_TAG, orderCashDistribution);
        }
        return ajax;
    }

    /**
     * 新增分销提现订单
     */
    @PostMapping("/orderCashDistributionCreate")
    public AjaxResult orderCashDistributionCreate(@Validated @RequestBody OrderCashDistribution orderCashDistribution, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderCashDistribution.setShopId(shopId);
        }
        Long userId = SecurityUtils.getUserId();
        CustomerUser cu = customerUserService.selectUserById(userId);
        customerUserService.checkUserAllowed(cu);
        return toAjax(orderCashDistributionService.insertOrderCashDistribution(orderCashDistribution));
    }

    /**
     * 修改分销提现订单
     */
    @PreAuthorize("@ss.hasPermi('order:cash:customer:edit')")
    @PostMapping("/orderCashDistributionEdit")
    public AjaxResult orderCashDistributionEdit(@Validated @RequestBody OrderCashDistribution orderCashDistribution)
    {
        return toAjax(orderCashDistributionService.updateOrderCashDistribution(orderCashDistribution));
    }
}
